﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
using System.Data;
using KPIS.Utils;
using KPIS.DBM;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.MODEL;
using Telerik.Web.UI;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PR_InComeSurchargeAnother : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private const int ItemsPerRequest = 10;
        private object _dataItem = null;
        private IPayrollsBLL payrollsBLL = new PayrollsBLL();
        private IIncomeTypeBLL extBLL = new IncomeTypeBLL();

        //ReadOnly property.
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }

        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }
        //Initializing the new variable updating ViewState data.
       protected override void OnLoad(System.EventArgs e)
       {
           base.OnLoad(e);

           txtAmount.Attributes.Add("onkeypress", "return isNumberKeyDot(event)");

           if (!IsUserControlPostBack)
           {
               this.ViewState.Add("IsUserControlPostBack", true);
           }
           if (DataItem != null)
           {
               if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
               {
                   lblID.Text = DataBinder.Eval(DataItem, "pay_dtls_seq").ToString();
                   cmbExtraType.Items.Add(new RadComboBoxItem(DataBinder.Eval(DataItem, "ext_name").ToString(), DataBinder.Eval(DataItem, "ext_seq").ToString()));
                   cmbExtraType.SelectedValue = DataBinder.Eval(DataItem, "ext_seq").ToString();
                   txtAmount.Text = DataBinder.Eval(DataItem, "amount", "{0:N2}").ToString();

                   this.ViewState.Add("IsUserControlPostBack", true);
               }
               else
               {

               }
           }
       }
       private static string GetStatusMessage(int offset, int total)
       {
           if (total <= 0)
               return "ไม่มีข้อมูล";

           return String.Format("รายการที่ <b>1</b>-<b>{0}</b> จากทั้งหมด <b>{1}</b> รายการ", offset, total);
       }
        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            try
            {
                bool dup = payrollsBLL.ChkDupPayrollsExtra(Int32.Parse(Request.QueryString["pay_seq"].ToString())
                    , Int32.Parse(Request.QueryString["emp_seq"].ToString())
                    , Int32.Parse(cmbExtraType.SelectedValue));
                if (dup != true)
                {
                    payrollsBLL.InsertPayrollsExtra(Int32.Parse(Session["pay_seq"].ToString())
                        , Int32.Parse(Session["emp_seq"].ToString())
                        , Int32.Parse(cmbExtraType.SelectedValue)
                        , Double.Parse(txtAmount.Text)
                        , ssMng.EmpSeq);
                }
            }
            catch(Exception ex)
            {
                
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                payrollsBLL.UpdatePayrollsExtra(Int32.Parse(lblID.Text), Int32.Parse(cmbExtraType.SelectedValue), Double.Parse(txtAmount.Text), ssMng.EmpSeq);
            }
            catch (Exception ex)
            {

            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }
        protected void cmbExtraType_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            cmbExtraType.Items.Clear();

            DataSet ds = extBLL.ListIncomeType(Int32.Parse(Session["EMP_TYPE_SEQ"].ToString()), SystemInfo._ActiveRecordStatus);
            DataTable dt = ds.Tables[0];

            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
            e.EndOfItems = (endOffset == dt.Rows.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbExtraType.Items.Add(new RadComboBoxItem(dt.Rows[i]["EXT_NAME"].ToString(), dt.Rows[i]["EXT_SEQ"].ToString()));

                TableRow tr = new TableRow();
                TableCell tdFieldName = new TableCell();
                Label lblFieldName = new Label();
                lblFieldName.Text = dt.Rows[i]["EXT_NAME"].ToString();
                tdFieldName.Width = Unit.Pixel(200);
                tdFieldName.Controls.Add(lblFieldName);
                tr.Cells.Add(tdFieldName);

                TableCell tdPosTypeName = new TableCell();
                Label lblPosTypeName = new Label();
                lblPosTypeName.Text = dt.Rows[i]["EXT_AMOUNT"].ToString();
                tdPosTypeName.Width = Unit.Pixel(100);
                tdPosTypeName.Controls.Add(lblPosTypeName);
                tr.Cells.Add(tdPosTypeName);

                ((Table)cmbExtraType.Items[j].FindControl("tableExtraType")).Controls.Add(tr);
                j++;
            }
            e.Message = GetStatusMessage(endOffset, dt.Rows.Count);
        }

        protected void cmbExtraType_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            DataSet ds = extBLL.ListIncomeType(Int32.Parse(cmbExtraType.SelectedValue));

            if (ds.Tables[0].Rows.Count > 0)
            {
                txtAmount.Text = string.Format("{0:N2}", ds.Tables[0].Rows[0]["ext_amount"]);
            }
            else
                txtAmount.Text = "";
        }

    }
}